package ie.dkit.java3Demulation.objects;

import java.util.ArrayList;
import java.util.Collection;

import ie.dkit.java3Demulation.drawer.EllipseDrawable;

public class Ellipse implements EllipseDrawable {

	private String name;

	private Point center_A;

	private Point center_B;

	private double radius;

	private Point center;

	private double radiusA, radiusB;

	private double _ang;

	public String getName() {
		return name;
	}

	public Ellipse() {
		this(new Point(), 50, 25, 0);
	}

	public Ellipse(Point c, int rad_A, int rad_B, double angle) {
		if (angle == 90) {
			angle = Math.PI/2;
		}else{
		angle = Math.toRadians(angle);
		}
		angle = angle % Math.PI;
		if (angle < 0) {
			angle = angle + Math.PI;
			angle = angle % Math.PI;
		}
		if (rad_A < rad_B) {
			radiusA = rad_B;
			radiusB = rad_A;
			angle = angle + Math.PI / 2;
			angle = angle % Math.PI;
		} else {
			radiusA = rad_A;
			radiusB = rad_B;
		}
		center = c;
		double distCF = Math.sqrt(Math.pow(radiusA, 2) - Math.pow(radiusB, 2));
		center_A = new Point(center.getX()
				- (Math.sin((Math.PI / 2) - angle) * distCF), center.getY()
				- (Math.cos((Math.PI / 2) - angle) * distCF));
		System.out.println(center_A.getX());
		System.out.println(center_A.getY());
		center_B = new Point(center.getX()
				+ (Math.sin((Math.PI / 2) - angle) * distCF), center.getY()
				+ (Math.cos((Math.PI / 2) - angle) * distCF));
		System.out.println(center_B.getX());
		System.out.println(center_B.getY());
		if (rad_A > rad_B) {
			radius = rad_A - (int) distCF;
		} else {
			radius = rad_B - (int) distCF;
		}
		System.out.println(radius);
		_ang = angle;
		System.out.println(_ang);
	}

	public Ellipse(Point cA, Point cB, int rad) {
		center_A = cA;
		center_B = cB;
		radius = rad;
	}

	public void setName(String name) {
		this.name = name;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see ie.dkit.java3Demulation.drawer.EclipseDrawable#getEclipses()
	 */
	public Collection<Ellipse> getEclipses() {
		Collection<Ellipse> returnValue = new ArrayList<Ellipse>();
		returnValue.add(this);
		return returnValue;
	}

	public Point getCenter_A() {
		return center_A;
	}

	public void setCenter_A(Point cA) {
		this.center_A = cA;
	}

	public Point getCenter() {
		Point pc = new Point((center_A.getX() + center_B.getX()) / 2, (center_A
				.getY() + center_B.getY()) / 2);
		return pc;
	}

	public Point getCenter_B() {
		return center_B;
	}

	public void setCenter_B(Point cB) {
		this.center_B = cB;
	}

	public double getRadius() {
		return radius;
	}

	public void setRadius(int rad) {
		this.radius = rad;
	}

	public double getAngle() {
		return _ang;
	}

	public void setAngle(double angle) {
		angle = Math.toRadians(angle);
		angle = angle % Math.PI;
		if (angle < 0) {
			angle = angle + Math.PI;
			angle = angle % Math.PI;
		}
		double distCF = 0;
		if (radiusA > radiusB) {
			distCF = Math.sqrt(Math.pow(radiusA, 2) - Math.pow(radiusB, 2));
		} else {
			distCF = Math.sqrt(Math.pow(radiusB, 2) - Math.pow(radiusA, 2));
		}
		center_A = new Point(center.getX()
				- (Math.sin((Math.PI / 2) - angle) * distCF), center.getY()
				- (Math.cos((Math.PI / 2) - angle) * distCF));
		center_B = new Point(center.getX()
				+ (Math.sin((Math.PI / 2) - angle) * distCF), center.getY()
				+ (Math.cos((Math.PI / 2) - angle) * distCF));
		if (radiusA > radiusB) {
			radius = radiusA - (int) distCF;
		} else {
			radius = radiusB - (int) distCF;
		}
		_ang = angle;
	}

	public double getRadiusA() {
		return radiusA;
	}

	public void setRadiusA(double radius) {
		if (radius < radiusB) {
			radiusA = radius;
			radiusB = radiusA;
			_ang = _ang + Math.PI / 2;
			_ang = _ang % Math.PI;
		} else {
			radiusA = radius;
		}
		double distCF = Math.sqrt(Math.pow(radiusA, 2) - Math.pow(radiusB, 2));
		center_A = new Point(center.getX()
				- (Math.sin((Math.PI / 2) - _ang) * distCF), center.getY()
				- (Math.cos((Math.PI / 2) - _ang) * distCF));
		center_B = new Point(center.getX()
				+ (Math.sin((Math.PI / 2) - _ang) * distCF), center.getY()
				+ (Math.cos((Math.PI / 2) - _ang) * distCF));
	}

	public double getRadiusB() {
		return radiusB;
	}

	public void setRadiusB(double radius) {
		if (radiusA < radius) {
			radiusA = radius;
			radiusB = radiusA;
			_ang = _ang + Math.PI / 2;
			_ang = _ang % Math.PI;
		} else {
			radiusB = radius;
		}
		double distCF = Math.sqrt(Math.pow(radiusA, 2) - Math.pow(radiusB, 2));
		center_A = new Point(center.getX()
				- (Math.sin((Math.PI / 2) - _ang) * distCF), center.getY()
				- (Math.cos((Math.PI / 2) - _ang) * distCF));
		center_B = new Point(center.getX()
				+ (Math.sin((Math.PI / 2) - _ang) * distCF), center.getY()
				+ (Math.cos((Math.PI / 2) - _ang) * distCF));
	}
}